<template>
  <div>
    <div class="headers">
      <el-button type="primary" size="mini" @click="frame">新增</el-button>
    </div>

    <el-table :data="tableData" border style="width: 100%" align="center">
      <el-table-column type="index" label="序号" width="80" align="center"></el-table-column>
      <el-table-column prop="companyName" label="对接公司" align="center"></el-table-column>

      <el-table-column prop="regionName" label="外部地区名称" align="center"></el-table-column>

      <el-table-column prop="devNum" label="关联楼盘" align="center">
        <template slot-scope="scope">
          <el-tag @click="Jump(scope.row)">{{ scope.row.devNum }}</el-tag>
        </template>
      </el-table-column>

      <el-table-column prop="updateName" label="操作人" align="center"></el-table-column>

      <el-table-column prop="updateTime" label="操作时间" align="center"></el-table-column>

      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button @click="handleEdit(scope.row)" class="btns">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currpage"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="100"
        layout="total, sizes, prev, pager, next, jumper"
        :total="nums"
      ></el-pagination>
    </div>
    <div v-if="flag">
      <Frame
        v-on:listenToChildEvent="showMsgfromChild"
        :headers="headers"
        :current="items"
        v-on:getlist="func"
      ></Frame>
    </div>
  </div>
</template>
<script>
import Frame from "./component/index";
export default {
  name: "externalArea",
  title: "外部地区管理",
  created() {
    console.log(this.$format);
    this.getdata();
  },
  data() {
    return {
      currpage: 1,
      pagesize: 10,
      flag: false,
      headers: "",
      items: {},
      tableData: [],
      nums: null
    };
  },
  methods: {
    Jump(items) {
      if (items.devNum > 0) {
        this.$router.push({
          path: "/projectmap",
          query: {
            outsideRegionId: items.id
          }
        });
      }
    },
    func() {
      this.getdata();
      this.$forceUpdate();
    },
    handleSizeChange(val) {
      this.pagesize = val;
      this.currpage = 1;
      this.getdata(val);
    },
    handleCurrentChange(val) {
      this.currpage = val;
      this.getdata();
    },
    handleEdit(current) {
      this.headers = "编辑";
      this.flag = true;
      this.items = current;
    },
    frame() {
      this.items = {};
      this.headers = "新增";
      this.flag = true;
    },
    showMsgfromChild(data) {
      this.flag = data;
    },

    getdata() {
      // 外部区域信息列表
      this.$axios
        .post("/commission/externalRegion/list", {
          pageSize: this.pagesize,
          pageNum: this.currpage
        })
        .then(res => {
          res.list.forEach((item, index) => {
            item.updateTime = this.formatTime(item.updateTime);
          });
          this.nums = res.total;
          this.tableData = res.list;
        });
    },
    formatTime(datetime) {
      var date = new Date(datetime);
      const year = date.getFullYear();
      const month = date.getMonth() + 1;
      const day = date.getDate();
      const hour = date.getHours();
      const minute = date.getMinutes();
      const second = date.getSeconds();
      return (
        [year, month, day].map(this.formatNumber).join("-") +
        " " +
        [hour, minute, second].map(this.formatNumber).join(":")
      );
    },
    formatNumber(n) {
      n = n.toString();
      return n[1] ? n : "0" + n;
    }
  },
  components: {
    Frame
  }
};
</script>
<style lang="stylus" scoped>
.headers {
  width: 100%;
  height: 60px;
  display: flex;
  justify-content: flex-end;
  align-items: center;

  button {
    margin-right: 5px;
  }
}

.block {
  height: 60px;
  display: flex;
  justify-content: flex-end;
  align-items: center;
}

.btns, .el-tag {
  border: none;
  text-decoration: underline;
  color: blue;
  background: none;
  cursor: pointer;
}
</style>